package a1_p04_ak_fh;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;

import javax.swing.DefaultDesktopManager;

import org.jgraph.graph.DefaultEdge;
import org.jgrapht.DirectedGraph;
import org.jgrapht.Graph;
import org.jgrapht.graph.DefaultDirectedGraph;
import org.jgrapht.graph.ListenableUndirectedGraph;
import org.jgrapht.graph.SimpleGraph;

public class IOHelper {

	/**
	 * readGraphFile
	 * @param filePath - Path to GraphFile
	 * @return Graph from file
	 */
	public Graph readGraphFile(String filePath) {

		Graph<String, DefaultEdge> graph = null;

		try {
			FileInputStream fstream = new FileInputStream(filePath);
			// Get the object of DataInputStream
			DataInputStream in = new DataInputStream(fstream);
			BufferedReader br = new BufferedReader(new InputStreamReader(in));
			String strLine;
			// Read File Line By Line
			strLine = br.readLine();

			// read graph type
			if (strLine.equals("#gerichtet")) {
				//Create directed graph
				graph = new DefaultDirectedGraph<String, DefaultEdge>(
						DefaultEdge.class);
				//Create graph from File
				while ((strLine = br.readLine()) != null) {
					String[] splitted = strLine.split("\\,");
					//Add vertices
					graph.addVertex(splitted[0]);
					graph.addVertex(splitted[1]);
					//Add edge
					graph.addEdge(splitted[0], splitted[1]);
					
					}
			} else if (strLine.equals("#ungerichtet")) {
				
				//Create undirected graph
				graph = new SimpleGraph <String, DefaultEdge>(DefaultEdge.class);
				while ((strLine = br.readLine()) != null) {

					String[] splitted = strLine.split(",");
					//add vertices
					graph.addVertex(splitted[0]);
					graph.addVertex(splitted[1]);
					//loops are not allowed in undirected graphs
					if(!splitted[1].equals(splitted[0])) {
						//add edge
						graph.addEdge(splitted[0], splitted[1]);
					}
					
				}
			} else {
				//Error while reading file
				exception();
			}


			// Close the input stream
			in.close();
		} catch (Exception e) {// Catch exception if any
			System.err.println("Error: " + e.getMessage());
		}
		return graph;
	}

	private void exception() throws Exception {
		throw new Exception("Wrong File Format");
	}
	
	
	/**
	 * writeGraphFile
	 * @param graph - Graph to write in file
	 * @param filePath - Path to put file to
	 */
	public void writeGraphFile(Graph<String, DefaultEdge> g, String filePath) {
		try {
			//Open File Streams
			FileOutputStream fstream = new FileOutputStream(filePath);
			DataOutputStream out = new DataOutputStream(fstream);
			BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(out));
			
			//Check if Graph is directed or undirected
			if(g instanceof DefaultDirectedGraph) {
				bw.append("#gerichtet");
				bw.newLine();
			} else if (g instanceof SimpleGraph) {
				bw.append("#ungerichtet");
				bw.newLine();
			}
			//Read graph edges and write them to graphfile
			for (DefaultEdge edge : g.edgeSet()) {
				bw.append(g.getEdgeSource(edge) + "," + g.getEdgeTarget(edge));
				bw.newLine();
			}
			
			//Flush all changes to file
			bw.flush();
			
			//Close file stram
			out.close();
			
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}

